package com.arpa.oms.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.arpa.core.web.Result;
import com.arpa.ntocc.common.common.constant.StrConsts;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.oms.service.IOmsInboundService;
import com.arpa.oms.service.IOmsInventoryService;
import com.arpa.oms.service.IOmsOutboundService;
import com.arpa.wms.cache.WarehouseCache;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * OMS首页报表接口
 * </p>
 *
 * @author LEO
 * @since 2020-08-03
 */
@Api(tags = "Oms首页图表")
@RestController
@RequestMapping(value = "/omsHome", produces = "application/json;charset=UTF-8")
@Log4j2
public class OmsHomeController {

    @Autowired
    private IOmsInventoryService omsInventoryService;
    private final IOmsInboundService iOmsInboundService;
    private final IOmsOutboundService iOmsOutboundService;
    private final WarehouseCache warehouseCache;

    public OmsHomeController(
            WarehouseCache warehouseCache,
            IOmsInboundService iOmsInboundService,
            IOmsOutboundService iOmsOutboundService) {
        this.warehouseCache = warehouseCache;
        this.iOmsInboundService = iOmsInboundService;
        this.iOmsOutboundService = iOmsOutboundService;
    }


    /**
     * 首页出库折线图数据
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "首页出库折线图数据", notes = "首页出库折线图数据")
    @GetMapping("lineData")
    public Result lineData(HttpServletRequest request) {
        try {
            //当前选择的
            String shipmentCode = UserUtil.getLoginWarehouseCodeOrShipmentCode();
            //客户绑定并授权的仓库
//            List<String> warehouseCodes = UserUtil.getShipmentAuthorizeWarehouseCodes();
            List<String> warehouseCodes = UserUtil.getAuthorizeWarehouseCodes();
            //按时间、仓库分组获取入库数量
            List<Map<String, Object>> outbountCount = iOmsOutboundService.outboundCount(shipmentCode,warehouseCodes);
            List<String> dateList = getDateList();

            List<Object> titleList = new ArrayList<>();
            List<String> xData = new ArrayList<>();
            List<List<Object>> yData = new ArrayList<>();

            warehouseCodes.forEach(r->{
                List<Object> countSet = new ArrayList<>();
                String warehouseName = warehouseCache.translate(r);
                titleList.add(warehouseName);
                dateList.forEach(k -> {
                    if (!xData.contains(k)) {
                        xData.add(k);
                    }
                    Map<String, Object> count = outbountCount.stream().filter(m ->
                            StrUtil.equals(k, MapUtil.getStr(m, "dateStr")) &&
                                    StrUtil.equals(r, MapUtil.getStr(m, "warehouse_code")))
                            .findFirst().orElse(null);
                    if (MapUtil.isNotEmpty(count)) {
                        Double quantity = MapUtil.getDouble(count, "quantity");
                        if (quantity != null) {
                            countSet.add(quantity);
                        } else {
                            countSet.add(0);
                        }
                    } else {
                        countSet.add(0);
                    }
                });
                yData.add(countSet);
            });
            JSONObject resultData = new JSONObject();
            resultData.putOpt("title", titleList);
            resultData.putOpt("xData", xData);
            resultData.putOpt("yData", yData);
            return Result.ok().data(resultData);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 首页入库柱状图数据
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "首页入库柱状图数据", notes = "首页入库柱状图数据")
    @GetMapping("barData")
    public Result barData(HttpServletRequest request) {
        try {
            //当前选择的
            String shipmentCode = UserUtil.getLoginWarehouseCodeOrShipmentCode();
            //客户绑定并授权的仓库
//            List<String> warehouseCodes = UserUtil.getShipmentAuthorizeWarehouseCodes();
            List<String> warehouseCodes = UserUtil.getAuthorizeWarehouseCodes();
            //按时间、仓库分组获取入库数量
            List<Map<String, Object>>   inbountCount = iOmsInboundService.inboundCount(shipmentCode,warehouseCodes);


            List<String> dateList = getDateList();


            List<Object> titleList = new ArrayList<>();
            List<String> xData = new ArrayList<>();
            List<List<Object>> yData = new ArrayList<>();

            warehouseCodes.forEach(r->{
                List<Object> countSet = new ArrayList<>();
                String warehouseName = warehouseCache.translate(r);
                titleList.add(warehouseName);
                dateList.forEach(k -> {
                    if (!xData.contains(k)) {
                        xData.add(k);
                    }
                    Map<String, Object> count = inbountCount.stream().filter(m ->
                            StrUtil.equals(k, MapUtil.getStr(m, "dateStr")) &&
                                    StrUtil.equals(r, MapUtil.getStr(m, "warehouse_code")))
                            .findFirst().orElse(null);
                    if (MapUtil.isNotEmpty(count)) {
                        Double quantity = MapUtil.getDouble(count, "quantity");
                        if (quantity != null) {
                            countSet.add(quantity);
                        } else {
                            countSet.add(0);
                        }
                    } else {
                        countSet.add(0);
                    }
                });
                yData.add(countSet);
            });
            JSONObject resultData = new JSONObject();
            resultData.putOpt("title", titleList);
            resultData.putOpt("xData", xData);
            resultData.putOpt("yData", yData);
            return Result.ok().data(resultData);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }


    /**
     * 首页入库柱状图数据
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "饼图", notes = "饼图")
    @GetMapping("pieData")
    public Result pieData(HttpServletRequest request) {
        try {
            //当前选择的
            String shipmentCode = UserUtil.getLoginWarehouseCodeOrShipmentCode();
            //客户绑定并授权的仓库
//            List<String> warehouseCodes = UserUtil.getShipmentAuthorizeWarehouseCodes();
            List<String> warehouseCodes = UserUtil.getAuthorizeWarehouseCodes();
            List<Map<String, Object>> inventoryCount = omsInventoryService.inventoryCount(shipmentCode,warehouseCodes);

            return Result.ok().data(inventoryCount);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }


    public List<String> getDateList(){
        List<String> dateList = new ArrayList<>();
        dateList.add(DateUtil.formatDate(DateUtil.offsetDay(DateUtil.date(), -6)));
        dateList.add(DateUtil.formatDate(DateUtil.offsetDay(DateUtil.date(), -5)));
        dateList.add(DateUtil.formatDate(DateUtil.offsetDay(DateUtil.date(), -4)));
        dateList.add(DateUtil.formatDate(DateUtil.offsetDay(DateUtil.date(), -3)));
        dateList.add(DateUtil.formatDate(DateUtil.offsetDay(DateUtil.date(), -2)));
        dateList.add(DateUtil.formatDate(DateUtil.offsetDay(DateUtil.date(), -1)));
        dateList.add(DateUtil.today());
        return dateList;
    }


}
